package com.rent.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rent.product.dto.PmsSpuInfoDto;
import com.rent.product.entity.PmsBrandEntity;
import com.rent.product.entity.PmsSkuInfoEntity;
import com.rent.product.entity.PmsSpuInfoEntity;
import com.rent.product.service.PmsBrandService;
import com.rent.product.service.PmsCategoryService;
import com.rent.product.service.PmsSkuInfoService;
import com.rent.product.service.PmsSpuInfoService;
import com.rent.product.utils.R;
import org.apache.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping(value = "/product/management")
public class ProductManagementController {

    @Resource
    private PmsSkuInfoService pmsSkuInfoService;

    /**
     * 根据查询条件返回符合条件的sku数据
     * @param page
     * @param limit
     * @param classificationId
     * @param brandId
     * @param price_start
     * @param price_end
     * @param search_text
     * @return
     */
    @RequestMapping(value = "/giveshelveslists", method = RequestMethod.GET)
    public R giveShelvesLists(Integer page, Integer limit, Long classificationId,
                              Long brandId, BigDecimal price_start, BigDecimal price_end, String search_text){
        Boolean muchWrapper = Boolean.TRUE;
        QueryWrapper<PmsSkuInfoEntity> wrapper = new QueryWrapper<PmsSkuInfoEntity>();
        if(classificationId != null){
            muchWrapper = Boolean.FALSE;
            wrapper.eq("catalog_id",classificationId);
        }
        if(brandId != null){
            muchWrapper = Boolean.FALSE;
            wrapper.eq("brand_id",brandId);
        }
        if(price_start != null && price_end != null && price_start.compareTo(price_end) == -1){
            muchWrapper = Boolean.FALSE;
            wrapper.ge("price", price_start).le("price", price_end);
        }
        if(search_text != null && !search_text.equals("")){
            muchWrapper = Boolean.FALSE;
            wrapper.like("sku_name",search_text);
        }
        List<PmsSkuInfoEntity> pmsSkuInfoEntityList;
        if(muchWrapper)
            pmsSkuInfoEntityList = pmsSkuInfoService.page(new Page<>(page,limit)).getRecords();
        else
            pmsSkuInfoEntityList = pmsSkuInfoService.page(new Page<>(page,limit), wrapper).getRecords();
        HashMap<String,Object> stringObjectHashMap = new HashMap<String,Object>();
        stringObjectHashMap.put("code",0);
        stringObjectHashMap.put("msg","");
        stringObjectHashMap.put("data",pmsSkuInfoEntityList);
        if(muchWrapper)
            stringObjectHashMap.put("count",pmsSkuInfoService.count());
        else
            stringObjectHashMap.put("count",pmsSkuInfoService.count(wrapper));
        return R.ok(stringObjectHashMap);
    }
}
